﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyPS.Models;
using MyPSDALayer;
using MyPS.CommonLib;

namespace MyPS.Controllers
{
    public class FeedbackController : Controller
    {
        MyPSEntities db = new MyPSEntities();
        //
        // GET: /Feedback/   
        public ActionResult Index()
        {
            ViewBag.Flag = "GET";
            return View();
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Index(Message message, string code)
        {
            if (ModelState.IsValid)
            {
                if (code == Session["ValidateCode"].ToString())
                {
                    message.CreateDate = DateTime.Now;
                    db.Messages.Add(message);
                    db.SaveChanges();
                    ViewBag.Flag = "POST";
                    return View();
                    //Response.Write(string.Format("<script>alert('{0}');</script>", "提交成功!"));
                }
                else
                {
                    ModelState.AddModelError("", "验证码不匹配");
                }
            }
            return View(message);
        }

        public ActionResult GetValidateCode()
        {
            ValidateCode vCode = new ValidateCode();
            string code = vCode.CreateValidateCode(5);
            Session["ValidateCode"] = code;
            byte[] bytes = vCode.CreateValidateGraphic(code);
            return File(bytes, @"image/jpeg");
        }
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
